<template>
  <div class="container">
    <div class="header">
      <span>当前城市 - {{ text }}</span>
    </div>
    <div class="search">
      <input type="text" placeholder="请输入城市名或拼音" />
    </div>
    <div class="hot">
      <p>GPS定位你所在城市</p>
      <button>武汉</button>
      <p>热门城市</p>
      <div class="hot-city">
        <button
          @click="changeCity(item.name)"
          v-for="item in hotCities"
          :key="item.id"
        >
          {{ item.name }}
        </button>
      </div>
    </div>
    <div class="city" v-for="(item, index) of city" :key="index">
      <p>{{ Object.keys(item)[0] }}</p>
      <div class="city-list">
        <ul>
          <li
            v-for="cityValue of item[Object.keys(item)[0]]"
            :key="cityValue.id"
            @click="changeCity(cityValue.name)"
          >
            {{ cityValue.name }}
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      text: "",
      hotCities: [],
      city: [],
    };
  },
  mounted() {
    this.text = this.$route.params.text;
    this.$http.get("http://47.108.197.28:8000/city").then((response) => {
      this.hotCities = response.data.data.hotCities;
      var cities = response.data.data.cities;
      for (var key in cities) {
        this.city.push({ [key]: cities[key] });
      }
    });
  },
  methods: {
    changeCity(val) {
       this.$store.dispatch("changeCity",val);
      this.$router.back();
    },
  },
};
</script>

<style scoped>
.container {
  width: 100%;
  background-color: #f0f0f0;
}
.header {
  text-align: center !important;
  height: 50px;
  line-height: 50px;
  background-color: white;
}
.header span {
  font-size: 20px;
}
.search {
  width: 100%;
  height: 35px;
  margin-top: 10px;
  text-align: center;
}
input {
  width: 300px;
  box-sizing: border-box;
  padding-left: 15px;
  background-color: white;
  border: 0;
  height: 30px;
  outline: none;
}
.hot {
  width: 100%;
  height: 210px;
  background-color: white;
  box-sizing: border-box;
  padding: 15px 0 0 15px;
  margin-top: 5px;
}
.hot p {
  color: #999;
}
button {
  width: 98px;
  height: 30px;
  margin: 8px 7.5px 15px 7.5px;
  border: 0;
  background-color: #f0f0f0;
}
.city {
  width: 100%;
}
.city p {
  margin: 5px 0;
  padding-left: 15px;
}
.city-list {
  background-color: white;
}
.city-list li {
  box-sizing: border-box;
  padding-left: 15px;
  height: 48px;
  line-height: 48px;
}
</style>